"""
Case Type   : 问题回归
Case Name   : B模式数据库下调用left,right系统函数
Create At   : 2023/02/18
Owner       : cr13
Description :
    1.创建B库
    2.切换至兼容B库,调用left,right函数
    3.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.commsh = CommonSH('PrimaryDbUser')
        self.database = 'd_bug_fix_049'

    def test_bug_fix(self):
        text = "----step1:创建B库 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database};" \
            f"create database {self.database} dbcompatibility ='B';"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, result,
                      '执行失败' + text)

        text = "----step2:切换至兼容B库,调用left,right函数 expect:成功----"
        self.log.info(text)
        sql_cmd = f"select right('testste',2) as result;" \
            f"select left('updatetext',3) as result;"
        result = self.commsh.execut_db_sql(sql_cmd, dbname=self.database)
        self.log.info(result)
        self.assertEqual(str(result.splitlines()[-2]).strip(), 'upd',
                         '执行失败' + text)
        self.assertEqual(str(result.splitlines()[-7]).strip(), 'te',
                         '执行失败' + text)

    def tearDown(self):
        text = '-----step4:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database {self.database};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
